using System;
using System.Data;
using System.Linq;
using ECatalog.Models.Organizations;
using ECatalog.Models.UserIU;
using ECatalog.Models.UserIU.Dto;

namespace ECatalog.Models.Models
{
    public class ModelRepository : IModelRepository
    {
        private readonly IOrganizationRepository organizationRepository;

        public ModelRepository(IOrganizationRepository organizationRepository)
        {
            this.organizationRepository = organizationRepository;
        }

        public Model AddModel(Model model)
        {
            using (var db = new DbContainer())
            {
                try
                {
                    db.Models.Add(model);
                    db.SaveChanges();
                }
                catch (Exception)
                {
                }
                return model;
            }
        }

        public Model[] GetAllModels()
        {
            using (var db = new DbContainer())
            {
                return db.Models.ToArray();
            }
        }

        public Model GetModelById(int id)
        {
            using (var db = new DbContainer())
            {
                return db.Models.FirstOrDefault(u => u.ModelId == id);
            }
        }

        public void RemoveModelById(int id)
        {
            using (var db = new DbContainer())
            {
                var model = db.Models.FirstOrDefault(u => u.ModelId == id);
                if (model != null)
                {
                    try
                    {
                        db.Models.Remove(model);
                        db.SaveChanges();
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }

        public void SaveModel(Model model)
        {
            using (var db = new DbContainer())
            {
                try
                {
                    db.Entry(model).State = EntityState.Modified;
                    db.SaveChanges();
                }
                catch (Exception)
                {
                }
            }
        }

        public ModelView GetViewModel(int modelId)
        {
            using (var db = new DbContainer())
            {
                var m = db.Models.FirstOrDefault(u => u.ModelId == modelId);
                if (m != null)
                {
                    var modelSizes = db.ModelSizes.Include("Size").Where(x => x.ModelId == modelId).ToArray();
                    var shops = db.Shops.Where(sh => sh.OrganizationId == m.OrganizationId).ToArray();
                    var colors = m.Colors.Where(col => col.OrganizationId == m.OrganizationId).ToArray();
                    var shopModelSizes = shops.Select(shop => new ShopModelSizes
                        {
                            Shop = shop,
                            ModelSizeDtos = modelSizes.Where(ms => ms.ShopId == shop.ShopId).Select(ms => new ModelSizeDto
                                {
                                    Value = ms.Size.Value,
                                    ModelSizeId = ms.ModelSizeId,
                                    SizeId = ms.SizeId
                                }).ToArray()
                        }).ToArray();
                    return new ModelView
                        {
                            CatNumber = m.CatNumber,
                            Price = m.Price,
                            Sale = m.Sale,
                            SalePrice = m.SalePrice,
                            ImageUrl = m.ImageUrl,
                            Structures = m.Structures.ToArray(),
                            Colors = colors,
                            Description = m.Description,
                            ModelId = m.ModelId,
                            KitModels = m.KitModels,
                            Locations = m.Locations,
                            Organization = m.Organization,
                            OrganizationId = m.OrganizationId,
                            UploadedFile = null,
                            StatModels = m.StatModels,
                            Organizations = organizationRepository.All(),
                            Shops = shops,
                            TypeImage = m.TypeImage,
                            ShopModelSizes = shopModelSizes
                        };
                }
                return new ModelView
                    {
                        Organizations = organizationRepository.All()
                    };
            }
        }

        public Model GetModelByNumber(string numModel)
        {
            using (var db = new DbContainer())
            {
                var model = db.Models.Include("KitModels").FirstOrDefault(m => m.CatNumber == numModel);
                return model;
            }
        }

        public bool ExistCatNumber(string catNumber)
        {
            using (var db = new DbContainer())
            {
                var model = db.Models.Any(m => m.CatNumber == catNumber);
                return model;
            }
        }

        public Model[] GetModelsByKitIds(int[] kitIds)
        {
            using (var db = new DbContainer())
            {
                var modelsIds = db.KitModels.Where(km => kitIds.Contains(km.KitId)).Select(km => km.ModelId);
                var models = db.Models.Include("KitModels").Include("Colors").Include("Structures").Include("ModelSizes").Include("Locations").Where(m => modelsIds.Contains(m.ModelId)).ToArray();
                return models;
            }
        }
    }
}